
import os
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


context = None
def init(context_):
    global context
    context = context_

class ActionNewNode:
    def __init__(self, kind, node):
        self._kind = kind
        self._node = node
    def __del__(self):
        pass
    def on_add_file(self, value):
        global context
        # 通过文件对话框选择文件
        fileName_choose, filetype = QFileDialog.getOpenFileName(None,  
            "选取文件",  
            "", # 起始路径 
            "All Files (*);;C Files (*.c)")   
        print(fileName_choose, filetype)
        if fileName_choose=="":
            return 

        # 文件名称
        file_name = os.path.basename(fileName_choose)

        # 创建对应的文件对象
        print("on_new_node")

        data_service = context.find_service("DataService")
        new_node = data_service.create_node("File", file_name)
        new_node.set_prop("path", fileName_choose)
        self._node.add_child(new_node)


class DataExploreMenu:
    def __init__(self):
        self.menus = []
    def add_menu(self, menu, nodes):
        self.menus = []
    
        kind = ""
        if len(nodes) == 1:
            node = nodes[0]
            kind = node.get_kind()

            if kind != "Project" and kind != "Group":
                return

            action = menu.addAction("添加源码文件")

            obj = ActionNewNode(kind, node)
            self.menus.append(obj)
            action.triggered.connect(obj.on_add_file)


config = {
    'pluginid': 'UnitTest::UI::ImportFile',
    "extensions" : [
        {
            "name": "PL::DataExplore::Menu",
            "id": "PL::DataExplore::Menu::UT::AddSource",
            "impl": DataExploreMenu()
        }
    ]
}